﻿using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Xml.Linq;
//
using PerceptiveMCAPI.Types.Internal;
//
namespace PerceptiveMCAPI.Types
{
	public class foldersOutput : Api_BaseOutput
	{
		public List<foldersResults> result { get; set; }
		public foldersParms inputParms { get; private set; }
		// --------------------------------------------------------
		public foldersOutput( foldersInput input )
			: base( input )
		{
			inputParms = input.parms;
			result = new List<foldersResults>();
		}
		// --------------------------------------------------- tostring
		public override string ToString()
		{
			return string.Format( "folder listed: {0}", this.result.Count );
		}

		#region ================================================================== format setup

		// ---- serial
		internal void Format( string api_Request, string api_Response )
		{
			this.api_Request = api_Request;
			this.api_Response = api_Response;
			// ----------------------------------
			if ( this.api_OutputType == EnumValues.OutputType.JSON )
				outputJSON();
			else if ( this.api_OutputType == EnumValues.OutputType.XML )
				outputXML();
		}

		// ---- xmlrpc
		internal void Format( x_folders[] rpcResult )
		{
			this.api_Request = EnumValues.NotSupplied;
			this.api_Response = EnumValues.NotSupplied;
			// ------------------------
			this.result = FormatResults( rpcResult );
		}

		#endregion

		#region  ===================================================================== output result

		// ---------------------------------------------------------- outputXML
		private void outputXML()
		{
			XElement xml = XElement.Parse( this.api_Response );
			Api_Error error = apiSerial.GetXmlOutputError( xml );
			if ( error != null )
			{
				this.api_ErrorMessages.Add( error );
				return;
			}
			// ------------------------------------ get values
			DateTime dt; double dbl;
			try
			{
				IEnumerable<XElement> folders = xml.Elements( "struct" );
				foreach ( XElement item in folders )
				{
					foldersResults entry = new foldersResults();
					double.TryParse( item.Element( "folder_id" ).Value, out dbl );
					entry.folder_id = dbl;
					entry.name = item.Element( "name" ).Value;
					DateTime.TryParse( item.Element( "date_created" ).Value, out dt );
					entry.date_created = dt;
					entry.type = apiHelper.GetEnumFromString<EnumValues.folder_type>( item.Element( "type" ).Value );
					//
					this.result.Add( entry );
				}
			}
			// ----------------------------- error
			catch ( Exception ex )
			{
				this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
			}
		}

		// ---------------------------------------------------------- outputJSON
		private void outputJSON()
		{
			JavaScriptSerializer jss = new JavaScriptSerializer();

			try
			{
				if ( this.api_Response.Contains( EnumValues.JSONErrorString ) )
				{
					Api_Error err = jss.Deserialize<Api_Error>( this.api_Response );
					this.api_ErrorMessages.Add( err );
				}
				else
				{
					var jsonResult = jss.Deserialize<x_folders[]>( this.api_Response );
					this.result = FormatResults( jsonResult );
				}
			}
			catch ( Exception ex )
			{
				this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
			}
		}

		// ----------------------------------------------------------------------------- FormatResults
		private List<foldersResults> FormatResults( x_folders[] x_results )
		{
			DateTime dt;
			List<foldersResults> results = new List<foldersResults>();
			//
			foreach ( var entry in x_results )
			{
				var result = new foldersResults();
				result.folder_id = entry.folder_id;
				result.name = entry.name;
				DateTime.TryParse( entry.date_created, out dt );
				result.date_created = dt;
				result.type = apiHelper.GetEnumFromString<EnumValues.folder_type>( entry.type );
				//
				results.Add( result );
			}
			//
			return results;
		}
		#endregion
	}
}
